package com.example.scaffold.user.controller;

import com.example.scaffold.common.bean.PageResult;
import com.example.scaffold.common.result.Result;
import com.example.scaffold.user.bean.UserDTO;
import com.example.scaffold.user.bean.UserEditParam;
import com.example.scaffold.user.bean.UserQueryParam;
import com.example.scaffold.user.bean.UserVO;
import com.example.scaffold.user.service.UserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

/**
 * 用户管理控制器
 * 遵循阿里手册：控制器命名规范，接口设计规范
 *
 * @author example
 */
@RestController
@RequestMapping("/user")
@Tag(name = "用户管理", description = "用户相关操作接口")
@RequiredArgsConstructor
public class UserController {
    private static final Logger log = LoggerFactory.getLogger(UserController.class);
    
    private final UserService userService;

    /**
     * 根据ID查询用户
     *
     * @param id 用户ID
     * @return 用户信息
     */
    @GetMapping("/{id}")
    @Operation(summary = "根据ID查询用户", description = "通过用户ID获取用户详细信息")
    public Result<UserDTO> getUserById(
            @Parameter(description = "用户ID", required = true)
            @PathVariable Long id) {
        log.info("查询用户信息，用户ID:{}", id);
        return Result.success(userService.getUserById(id));
    }

    /**
     * 根据属性查询用户，带有分页
     *
     */
    @PostMapping("/page")
    @Operation(summary = "根据属性查询用户", description = "根据属性查询用户，带有分页")
    public Result<PageResult<UserVO>> listUser(@RequestBody UserQueryParam param) {
        log.info("根据属性查询用户，参数:{}", param);
        return Result.success(userService.listUser(param));
    }

    /**
     * 创建用户
     *
     * @param param 用户信息
     * @return 操作结果
     */
    @PostMapping
    @Operation(summary = "创建用户", description = "新增用户信息")
    public Result<Long> createUser(
            @Parameter(description = "用户信息", required = true)
            @Valid @RequestBody UserEditParam param) {
        log.info("创建用户，用户信息:{}", param.getUsername());
        Long userId = userService.createUser(param);
        log.info("创建用户成功，用户ID:{}", userId);
        return Result.success(userId);
    }

    /**
     * 更新用户
     *
     * @param id      用户ID
     * @param param 用户信息
     * @return 操作结果
     */
    @PutMapping("/{id}")
    @Operation(summary = "更新用户", description = "修改用户信息")
    public Result<Boolean> updateUser(
            @Parameter(description = "用户ID", required = true)
            @PathVariable Long id,
            @Parameter(description = "用户信息", required = true)
            @Valid @RequestBody UserEditParam param) {
        log.info("更新用户，用户ID:{}", id);
        return Result.success(userService.updateUser(id, param));
    }

    /**
     * 删除用户
     *
     * @param id 用户ID
     * @return 操作结果
     */
    @DeleteMapping("/{id}")
    @Operation(summary = "删除用户", description = "根据ID删除用户")
    public Result<Boolean> deleteUser(
            @Parameter(description = "用户ID", required = true)
            @PathVariable Long id) {
        log.info("删除用户，用户ID:{}", id);
        return Result.success(userService.deleteUser(id));
    }
}
    